package com.wnxy.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wnxy.admin.common.handler.Asserts;
import com.wnxy.admin.dto.StaffRoleDto;
import com.wnxy.admin.entity.RolePermission;
import com.wnxy.admin.entity.StaffRole;
import com.wnxy.admin.mapper.StaffRoleMapper;
import com.wnxy.admin.service.IStaffRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.admin.vo.StaffRoleVo;
import org.springframework.data.annotation.Id;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * 员工角色中间表 服务实现类
 * </p>
 *
 * @author All
 * @since 2023-11-29
 */
@Service
public class StaffRoleServiceImpl extends ServiceImpl<StaffRoleMapper, StaffRole> implements IStaffRoleService {

    private final StaffRoleMapper staffRoleMapper;

    public StaffRoleServiceImpl(StaffRoleMapper staffRoleMapper) {
        this.staffRoleMapper = staffRoleMapper;
    }

    @Override
    public List<StaffRoleVo> getAll(Integer id) {
        return staffRoleMapper.selectAll(id);
    }

    @Override
    public void authorizationStaff(StaffRoleDto staffRoleDto) {
        //先清理一次
        staffRoleMapper.delete(Wrappers.lambdaQuery(StaffRole.class)
                .eq(StaffRole::getRoleId, staffRoleDto.getId()));
        //加入数据库
        staffRoleDto.getStaffRoleDatas().forEach(e -> {
            StaffRole staffRole = new StaffRole();
            staffRole.setStaffId(e);
            staffRole.setRoleId(staffRoleDto.getId());
            staffRoleMapper.insert(staffRole);
        });
    }
}
